package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.base.ComponentContext;
import com.photoeditor.R;
import com.photoeditor.demo.model.bean.EditEmojiBean;
import com.photoeditor.demo.model.bean.stash.ContainerBean;
import com.photoeditor.demo.ui.widget.CheckableImageView;
import com.photoeditor.demo.ui.widget.CustomSimpleSeekBar;
import com.photoeditor.demo.ui.widget.image.emoji.CanvasEditEmojiView;
import com.photoeditor.demo.ui.widget.image.emoji.IStickerSetting;
import com.photoeditor.demo.ui.widget.image.hair.CustomSizeSeekBar;
import com.photoeditor.demo.util.FontUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/14 0014.
 */

public class StickerSettingView extends LinearLayout implements View.OnClickListener, com.photoeditor.demo.ui.widget.image.hair.OnSeekBarChangeListener, CanvasEditEmojiView.StickerEditSettingListener, StickerSettingTopView.StickerTopTabListener, CustomSimpleSeekBar.OnSeekBarChangeListener {

    private IStickerSetting mEditStickerView;

    private StickerSettingTopView mTopView;

    private CustomSimpleSeekBar mSeekBarTone;
    private CustomSimpleSeekBar mSeekBarSaturation;
    private CustomSimpleSeekBar mSeekBarAlpha;
    private CustomSimpleSeekBar mSeekBarBrightness;

    private CheckableImageView mCheckableImageViewAdjust;
    private CheckableImageView mCheckableImageViewPaint;
    private CheckableImageView mCheckableImageViewBrush;
    private CheckableImageView mCheckableImageViewErase;

    private TextView mTvTone;
    private TextView mTvSaturation;
    private TextView mTvAlpha;
    private TextView mTvBrightness;

    public static final int MODE_ADJUST = 0;
    public static final int MODE_PAINT = 1;
    private int mMode = MODE_PAINT;

    private CustomSizeSeekBar mSizeSeekBarPaint;

    private TextView mBtnStiffBrush;
    private TextView mBtnSoftBrush;

    private View mAdjustView;
    private View mPaintView;

    private Drawable mStiffDrawable, mStiffDrawableSelect;
    private Drawable mSoftDrawable, mSoftDrawableSelect;

    private Map<Integer, Integer> mOpTimeParamMap = new HashMap<>();
    private Map<Integer, Integer> mOpTimePaintMap = new HashMap<>();
    //哈希值
    private int mCurrentBean;

    public StickerSettingView(Context context) {
        this(context, null);
    }

    public StickerSettingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StickerSettingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mStiffDrawable = getResources().getDrawable(R.drawable.shape_stiff_normal);
        mStiffDrawable.setBounds(0, 0, mStiffDrawable.getMinimumWidth(), mStiffDrawable.getMinimumHeight());
        mStiffDrawableSelect = getResources().getDrawable(R.drawable.shape_stiff_selected);
        mStiffDrawableSelect.setBounds(0, 0, mStiffDrawableSelect.getMinimumWidth(), mStiffDrawableSelect.getMinimumHeight());
        mSoftDrawable = getResources().getDrawable(R.drawable.ic_sticker_setting_softbrush_off);
        mSoftDrawable.setBounds(0, 0, mSoftDrawable.getMinimumWidth(), mSoftDrawable.getMinimumHeight());
        mSoftDrawableSelect = getResources().getDrawable(R.drawable.ic_sticker_setting_softbrush_on);
        mSoftDrawableSelect.setBounds(0, 0, mSoftDrawableSelect.getMinimumWidth(), mSoftDrawableSelect.getMinimumHeight());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initView();
    }

    private void initView() {
        mAdjustView = findViewById(R.id.sticker_adjust_view);
        mPaintView = findViewById(R.id.sticker_paint_view);

        findViewById(R.id.exit).setOnClickListener(this);
        findViewById(R.id.confirm).setOnClickListener(this);
        mSeekBarTone = (CustomSimpleSeekBar) findViewById(R.id.seekbar_tone);
        mSeekBarSaturation = (CustomSimpleSeekBar) findViewById(R.id.seekbar_saturation);
        mSeekBarAlpha = (CustomSimpleSeekBar) findViewById(R.id.seekbar_alpha);
        mSeekBarBrightness = (CustomSimpleSeekBar) findViewById(R.id.seekbar_brightness);

        mSeekBarTone.setMaxProgress(360);

        mSeekBarTone.setBackGroundColor(new int[]{getColor(R.color.sticker_tone_color_1),
                getColor(R.color.sticker_tone_color_2),
                getColor(R.color.sticker_tone_color_3),
                getColor(R.color.sticker_tone_color_4),
                getColor(R.color.sticker_tone_color_5),
                getColor(R.color.sticker_tone_color_6),
                getColor(R.color.sticker_tone_color_7)});

        mSeekBarSaturation.setBackGroundColor(new int[]{getColor(R.color.sticker_saturation_color_1),
                getColor(R.color.sticker_saturation_color_2)});

        mSeekBarAlpha.setBackGroundColor(new int[]{getColor(R.color.sticker_alpha_color_1),
                getColor(R.color.sticker_alpha_color_2)});

        mSeekBarBrightness.setBackGroundColor(new int[]{getColor(R.color.sticker_brightness_color_1),
                getColor(R.color.sticker_brightness_color_2)});

        mSeekBarTone.setSeekBarChangeListener(this);
        mSeekBarSaturation.setSeekBarChangeListener(this);
        mSeekBarAlpha.setSeekBarChangeListener(this);
        mSeekBarBrightness.setSeekBarChangeListener(this);

        mTvTone = (TextView) findViewById(R.id.tv_tone);
        mTvSaturation = (TextView) findViewById(R.id.tv_saturation);
        mTvAlpha = (TextView) findViewById(R.id.tv_alpha);
        mTvBrightness = (TextView) findViewById(R.id.tv_brightness);

        mCheckableImageViewAdjust = (CheckableImageView) findViewById(R.id.adjust);
        mCheckableImageViewPaint = (CheckableImageView) findViewById(R.id.paint);

        mCheckableImageViewAdjust.setOnClickListener(this);
        mCheckableImageViewPaint.setOnClickListener(this);

        mCheckableImageViewBrush = (CheckableImageView) findViewById(R.id.sticker_switch_paint);
        mCheckableImageViewErase = (CheckableImageView) findViewById(R.id.sticker_switch_erase);

        mCheckableImageViewBrush.setOnClickListener(this);
        mCheckableImageViewErase.setOnClickListener(this);

        mSizeSeekBarPaint = (CustomSizeSeekBar) findViewById(R.id.seekbar_paint);
        mSizeSeekBarPaint.setOnSeekBarChangeListener(this);
        mSizeSeekBarPaint.setMax(100);

        mBtnStiffBrush = (TextView) findViewById(R.id.stiff_brush);
        mBtnSoftBrush = (TextView) findViewById(R.id.soft_brush);

        mBtnStiffBrush.setOnClickListener(this);
        mBtnSoftBrush.setOnClickListener(this);

        FontUtil.setCustomFont(mTvTone, mTvSaturation, mTvAlpha, mTvBrightness, mBtnStiffBrush, mBtnSoftBrush);

        reset();
    }

    private void checkBrush() {
        mCheckableImageViewBrush.setChecked(true);
        mCheckableImageViewErase.setChecked(false);
    }

    private void checkErase() {
        mCheckableImageViewBrush.setChecked(false);
        mCheckableImageViewErase.setChecked(true);
    }

    private void checkStiffBrush() {
        mBtnStiffBrush.setSelected(true);
        mBtnStiffBrush.setCompoundDrawables(mStiffDrawableSelect, null, null, null);
        mBtnSoftBrush.setSelected(false);
        mBtnSoftBrush.setCompoundDrawables(mSoftDrawable, null, null, null);
    }

    private void checkSoftBrush() {
        mBtnStiffBrush.setSelected(false);
        mBtnStiffBrush.setCompoundDrawables(mStiffDrawable, null, null, null);
        mBtnSoftBrush.setSelected(true);
        mBtnSoftBrush.setCompoundDrawables(mSoftDrawableSelect, null, null, null);

    }

    private void checkAdjustView() {
        mMode = MODE_ADJUST;
        mCheckableImageViewAdjust.setChecked(true);
        mCheckableImageViewPaint.setChecked(false);
        mAdjustView.setVisibility(View.VISIBLE);
        mPaintView.setVisibility(View.GONE);

        if (mTopView != null) {
            if (mOpTimeParamMap.get(mCurrentBean) >= 2) {
                mTopView.setTwoStepBtnShow(true);
                mTopView.setOneStepBtnShow(true);
            } else if (mOpTimeParamMap.get(mCurrentBean) >= 1) {
                mTopView.setOneStepBtnShow(true);
                mTopView.setTwoStepBtnShow(false);
            } else {
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
            }
        }
    }

    private void checkPaintView() {
        mMode = MODE_PAINT;
        mCheckableImageViewAdjust.setChecked(false);
        mCheckableImageViewPaint.setChecked(true);
        mAdjustView.setVisibility(View.GONE);
        mPaintView.setVisibility(View.VISIBLE);

        if (mTopView != null) {
            if (mOpTimePaintMap.get(mCurrentBean) >= 2) {
                mTopView.setTwoStepBtnShow(true);
                mTopView.setOneStepBtnShow(true);
            } else if (mOpTimePaintMap.get(mCurrentBean) >= 1) {
                mTopView.setOneStepBtnShow(true);
                mTopView.setTwoStepBtnShow(false);
            } else {
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
            }
        }
    }

    private int getColor(int id) {
        return ComponentContext.getContext().getResources().getColor(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.exit:
                setVisibility(View.GONE);
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
                mEditStickerView.setCanPaint(true);
                mEditStickerView.cancelSettingMode();
                if (mStickerSettingListener != null) {
                    mStickerSettingListener.onExit();
                }
                reset();
                break;
            case R.id.confirm:
                setVisibility(View.GONE);
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
                mEditStickerView.setCanPaint(true);
                mEditStickerView.saveSettingMode();
                if (mStickerSettingListener != null) {
                    mStickerSettingListener.onExit();
                }
                reset();
                break;
            case R.id.adjust:
                checkAdjustView();
                mEditStickerView.setCanPaint(false);
                break;
            case R.id.paint:
                checkPaintView();
                mEditStickerView.setCanPaint(true);
                break;
            case R.id.sticker_switch_paint:
                checkBrush();
                //擦除画笔
                mEditStickerView.setPaintMode(EditEmojiBean.PAINT_MODE_ERASE);
                break;
            case R.id.sticker_switch_erase:
                checkErase();
                //还原画笔
                mEditStickerView.setPaintMode(EditEmojiBean.PAINT_MODE_REVERSE);
                break;
            case R.id.stiff_brush:
                checkStiffBrush();
                mEditStickerView.setPaintStyle(EditEmojiBean.PAINT_STYLE_STIFF);
                break;
            case R.id.soft_brush:
                checkSoftBrush();
                mEditStickerView.setPaintStyle(EditEmojiBean.PAINT_STYLE_SOFT);
                break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }

    @Override
    public void onUndoClick() {
        if (mMode == MODE_ADJUST) {
            int i = mOpTimeParamMap.get(mCurrentBean);
            i--;
            if (i >= 2) {
                mTopView.setTwoStepBtnShow(true);
                mTopView.setOneStepBtnShow(true);
            } else if (i >= 1) {
                mTopView.setOneStepBtnShow(true);
                mTopView.setTwoStepBtnShow(false);
            } else {
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
            }
            mOpTimeParamMap.put(mCurrentBean, i);
            mEditStickerView.removeLastParam();
            ContainerBean bean = mEditStickerView.getCurrentBean();
            if (bean != null && bean instanceof EditEmojiBean) {
                EditEmojiBean emojiBean = (EditEmojiBean) bean;
                mSeekBarTone.setProgress((int) emojiBean.getTone());
                mSeekBarSaturation.setProgress((int) (emojiBean.getSaturation() * 50));
                mSeekBarAlpha.setProgress((int) (emojiBean.getAlpha() * 100));
                mSeekBarBrightness.setProgress((int) (emojiBean.getBrightness() * 50));
            }
        } else if (mMode == MODE_PAINT) {
            int i = mOpTimePaintMap.get(mCurrentBean);
            i--;
            if (i >= 2) {
                mTopView.setTwoStepBtnShow(true);
                mTopView.setOneStepBtnShow(true);
            } else if (i >= 1) {
                mTopView.setOneStepBtnShow(true);
                mTopView.setTwoStepBtnShow(false);
            } else {
                mTopView.setOneStepBtnShow(false);
                mTopView.setTwoStepBtnShow(false);
                resetPaint();
            }
            mOpTimePaintMap.put(mCurrentBean, i);
            mEditStickerView.removeLastPaint();
        }
    }

    @Override
    public void onResetClick() {
        if (mMode == MODE_ADJUST) {
            mOpTimeParamMap.put(mCurrentBean, 0);
            mTopView.setOneStepBtnShow(false);
            mTopView.setTwoStepBtnShow(false);
            resetParam();
            mEditStickerView.resetParam();
        } else if (mMode == MODE_PAINT) {
            mOpTimePaintMap.put(mCurrentBean, 0);
            mTopView.setOneStepBtnShow(false);
            mTopView.setTwoStepBtnShow(false);
            resetPaint();
            mEditStickerView.resetPaint();
        }

    }

    @Override
    public void onOriginClick(boolean show) {
        mEditStickerView.setShowSrc(show);
    }

    public void setEditStickerView(IStickerSetting editStickerView) {
        mEditStickerView = editStickerView;
        mCurrentBean = mEditStickerView.getCurrentBean().hashCode();
        mOpTimeParamMap.put(mCurrentBean, 0);
        mOpTimePaintMap.put(mCurrentBean, 0);
        mEditStickerView.setStickerEditSettingListener(this);
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == VISIBLE) {
            if (mEditStickerView != null) {
                if (mEditStickerView.getCurrentBean() == null) {
                    setVisibility(View.GONE);
                    mTopView.setOneStepBtnShow(false);
                    mTopView.setTwoStepBtnShow(false);
                    mEditStickerView.setCanPaint(true);
                    mEditStickerView.cancelSettingMode();
                    if (mStickerSettingListener != null) {
                        mStickerSettingListener.onExit();
                    }
                    reset();
                    return;
                }
                mCurrentBean = mEditStickerView.getCurrentBean().hashCode();
                if (!mOpTimeParamMap.containsKey(mCurrentBean)) {
                    mOpTimeParamMap.put(mCurrentBean, 0);
                }
                if (!mOpTimePaintMap.containsKey(mCurrentBean)) {
                    mOpTimePaintMap.put(mCurrentBean, 0);
                }
            }
        }
    }

    @Override
    public void onProgressChanged(CustomSizeSeekBar seekBar, int progress, boolean fromUser) {
        if (mEditStickerView == null) {
            return;
        }
        if (seekBar.getId() == R.id.seekbar_paint) {
            mEditStickerView.updatePaintWidthProgress(progress);
        }
    }

    @Override
    public void onStartTrackingTouch(CustomSizeSeekBar seekBar) {
        //显示画笔宽度
        mEditStickerView.setDrawPaintWidthCircle(true);
    }

    @Override
    public void onStopTrackingTouch(CustomSizeSeekBar seekBar) {
        //停止显示画笔宽度
        mEditStickerView.setDrawPaintWidthCircle(false);
    }

    public void reset() {
        resetOpTime();

        checkPaintView();

        resetParam();

        resetPaint();
    }

    private void resetOpTime() {
        for (Integer i : mOpTimeParamMap.keySet()) {
            mOpTimeParamMap.put(i, 0);
        }
        for (Integer i : mOpTimePaintMap.keySet()) {
            mOpTimePaintMap.put(i, 0);
        }
    }

    public void resetParam() {
        mSeekBarTone.setProgress(0);
        mSeekBarSaturation.setProgress(50);
        mSeekBarAlpha.setProgress(0);
        mSeekBarBrightness.setProgress(50);
    }

    public void resetPaint() {
        mSizeSeekBarPaint.setProgress(50);
        checkBrush();
        checkStiffBrush();
    }

    public void setTopView(StickerSettingTopView topView) {
        mTopView = topView;
        mTopView.setStickerTopTabListener(this);
    }

    private StickerSettingListener mStickerSettingListener;

    public void setStickerSettingListener(StickerSettingListener stickerSettingListener) {
        mStickerSettingListener = stickerSettingListener;
    }

    @Override
    public void onPaintFinish() {
        int i = mOpTimePaintMap.get(mCurrentBean);
        i++;
        if (i >= 2) {
            mTopView.setTwoStepBtnShow(true);
            mTopView.setOneStepBtnShow(true);
        } else if (i >= 1) {
            mTopView.setOneStepBtnShow(true);
            mTopView.setTwoStepBtnShow(false);
        } else {
            mTopView.setOneStepBtnShow(false);
            mTopView.setTwoStepBtnShow(false);
        }
        mOpTimePaintMap.put(mCurrentBean, i);

    }

    @Override
    public void onSelectOther(ContainerBean containerBean) {
        EditEmojiBean bean = null;
        if (containerBean instanceof EditEmojiBean) {
            bean = (EditEmojiBean) containerBean;
        }
        if (bean == null) {
            return;
        }
        mCurrentBean = bean.hashCode();
        if (!mOpTimeParamMap.containsKey(mCurrentBean)) {
            mOpTimeParamMap.put(mCurrentBean, 0);
        }
        if (!mOpTimePaintMap.containsKey(mCurrentBean)) {
            mOpTimePaintMap.put(mCurrentBean, 0);
        }
        int i = 0;
        if (mMode == MODE_ADJUST) {
            i = mOpTimeParamMap.get(mCurrentBean);
        } else {
            i = mOpTimePaintMap.get(mCurrentBean);
        }
        if (i >= 2) {
            mTopView.setTwoStepBtnShow(true);
            mTopView.setOneStepBtnShow(true);
        } else if (i >= 1) {
            mTopView.setOneStepBtnShow(true);
            mTopView.setTwoStepBtnShow(false);
        } else {
            mTopView.setOneStepBtnShow(false);
            mTopView.setTwoStepBtnShow(false);
        }

        mSeekBarTone.setProgress((int) bean.getTone());
        mSeekBarSaturation.setProgress((int) (bean.getSaturation() * 50));
        mSeekBarAlpha.setProgress((int) (bean.getAlpha() * 100));
        mSeekBarBrightness.setProgress((int) (bean.getBrightness() * 50));

        if (bean.getPaintMode() == EditEmojiBean.PAINT_MODE_REVERSE) {
            checkErase();
        } else {
            checkBrush();
        }

        if (bean.getPaintStyle() == EditEmojiBean.PAINT_STYLE_SOFT) {
            checkSoftBrush();
        } else {
            checkStiffBrush();
        }
        mSizeSeekBarPaint.setProgress(bean.getPaintWidthProgress());
    }

    @Override
    public void onProgressChanged(CustomSimpleSeekBar seekBar, int progress) {
        switch (seekBar.getId()) {
            case R.id.seekbar_tone:
                if (mEditStickerView != null) {
                    mEditStickerView.updateTone(progress);
                }
                mTvTone.setText(getResources().getString(R.string.sticker_setting_tone, (int) ((progress * 1.0f / 360) * 100)));
                break;
            case R.id.seekbar_saturation:
                if (mEditStickerView != null) {
                    mEditStickerView.updateSaturation(progress * 1.0F / 50);
                }
                mTvSaturation.setText(getResources().getString(R.string.sticker_setting_saturation, progress));
                break;
            case R.id.seekbar_alpha:
                if (mEditStickerView != null) {
                    mEditStickerView.updateAlpha(progress / (100 * 1.0f));
                }
                mTvAlpha.setText(getResources().getString(R.string.sticker_setting_alpha, progress));
                break;
            case R.id.seekbar_brightness:
                if (mEditStickerView != null) {
                    mEditStickerView.updateBrightness(progress * 1.0F / 50);
                }
                mTvBrightness.setText(getResources().getString(R.string.sticker_setting_brightness, progress));
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(CustomSimpleSeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(CustomSimpleSeekBar seekBar) {
        if (mEditStickerView == null) {
            return;
        }
        int i = mOpTimeParamMap.get(mCurrentBean);
        i++;
        if (i >= 2) {
            mTopView.setOneStepBtnShow(true);
            mTopView.setTwoStepBtnShow(true);
        } else if (i >= 1) {
            mTopView.setOneStepBtnShow(true);
            mTopView.setTwoStepBtnShow(false);
        } else {
            mTopView.setOneStepBtnShow(false);
            mTopView.setTwoStepBtnShow(false);
        }
        mOpTimeParamMap.put(mCurrentBean, i);
        mEditStickerView.saveParam();
    }

    public interface StickerSettingListener {
        void onExit();
    }
}
